package cn.com.dhcc.ddi.datasource.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.dao.SimpleDaoForOracle;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.datasource.vo.DataSourceVo;
import cn.com.dhcc.ddi.datasource.vo.TableInfoVo;
import cn.com.dhcc.ddi.datasource.vo.wapper.TableInfoRowWapper;

/**
 * @日期：2016/11/16 9:37
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Repository
public class TableInfoDao extends BaseDao{
	
	private SimpleDaoForOracle simpleDaoForOracle;

	@Autowired
	public DataSourceDao datasourceDao;

	/**
	 * 现根据界面上的连接信息初始化数据源
	 * 
	 * @param vo
	 * @throws NiceException
	 * @throws ServiceException 
	 * @throws DaoException 
	 */
	public void initDao(DataSourceVo vo) throws DaoException {
		try {
			simpleDaoForOracle = datasourceDao.initDao(vo);
		} catch (DaoException e) {
			throw new DaoException("初始化数据源错误", e);
		}
	}

    /**
     * 
     * @MethodName: [userTableDao]
     * @Description:[验证发布条件失败s]
     * @Author:     [fxx]
     * @CreateDate: [2017-5-16]
     * @Params:     [ParamType ParamValue,ParamType ParamValue...]
     * @See         [Related classes and methods]
     * @Since       [Product template version]  
     * @Return:     [long]
     * @Throws:     [Exception]
     *
     */
	public long userTableDao(String testSql) throws DaoException {
		String msg = "";
		try {
			Long num = simpleDaoForOracle.queryForLong("验证发布条件", testSql, new Object[] {});
			return num == null ? 0 : num.intValue();
		} catch (DaoException e) {
			msg = "验证发布条件失败";
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 获取数据源对应表信息
	 * @param  id 数据源id
	 * @return 数据源对应表信息
	 * @throws DaoException
	 */
	public List<TableInfoVo> getTableInfoByDsId(String id) throws DaoException {
		String msg = "";
		String sql = "SELECT * FROM  " + SI_TABLE("t_ddi_table_info")+" t1 WHERE t1.DS_ID = ? order by t1.table_name";

		try {
			return getDao().list("获取数据源对应表信息", sql, new TableInfoRowWapper(), new Object[]{id});
		} catch (DaoException e) {
			msg = "获取数据源对应表信息失败";
			throw new DaoException(msg, e);
		}
	}
	/**
	 * 
	 * @Title: insertTableInfo
	 * @Description: 数据源表信息入库
	 * @param @param tbInfoList
	 * @param @throws DaoException 参数
	 * @return void 返回类型
	 * @throws
	 */
	public int insertTableInfo(List<Object[]> tbInfoList) throws DaoException {
		String sql = "insert into "+ SI_TABLE("t_ddi_table_info")+ "(id,ds_id,table_name,schema_name,table_comment,table_order,mtime) values(?,?,?,?,?,?,?)";
		return CM.getDao().batchInsert("数据源表信息入库", sql, tbInfoList);		
	}
	
	/**
	 * 
	 * @Title: insertTableInfo
	 * @Description: 数据源表信息入库
	 * @param @param vo
	 * @param @return
	 * @param @throws DaoException    参数
	 * @return int    返回类型
	 * @throws
	 */
	public int insertTableInfo(TableInfoVo vo) throws DaoException {
		String sql = "insert into "+ SI_TABLE("t_ddi_table_info")+ "(id,ds_id,table_name,schema_name,table_comment,table_order,mtime) values(?,?,?,?,?,?,?)";
		return CM.getDao().insert("数据源表信息入库", sql, new Object[]{vo.getId(), vo.getDsId(), vo.getTableName(), vo.getSchema(), vo.getTableComment(),
				                  vo.getTableOrder(), vo.getMtime()});
	}

	/**
	 * 
	 * @Title: updateTableInfo
	 * @Description: 数据源表信息入库
	 * @param @param tbInfoList
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return int 返回类型
	 * @throws
	 */
	public int updateTableInfo(TableInfoVo vo) throws DaoException {
		String sql = "update "+ SI_TABLE("t_ddi_table_info")+ " set table_comment=?,  " +
				      " mtime=? where ds_id=? and schema_name=? and table_name=? ";
		return CM.getDao().update("数据源表信息入库", sql, new Object[]{ vo.getTableComment(), vo.getMtime(), vo.getDsId(), vo.getSchema(), vo.getTableName()});
	}
	
	/**
	 * 
	 * @Title: getTableIdByDsId
	 * @Description: 根据数据源id获取表id集合
	 * @param @param tableId
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return List<String> 返回类型
	 * @throws
	 */
    public List<String> getTableIdByDsId(String dsId) throws DaoException{
		String sql = "select id from "+ SI_TABLE("t_ddi_table_info")+ " where ds_id = ? ";
		return CM.getDao().listSignleStringColumn("", sql, new WhereCondition(), new Object[]{dsId});
	}
    
	/**
	 * 
	 * @Title: deleteTbInfoById
	 * @Description: 根据id删除表信息
	 * @param @param id
	 * @param @throws DaoException 参数
	 * @return void 返回类型
	 * @throws
	 */
    public int deleteTbInfoByDsId(String dsId) throws DaoException{
    	String sql = "delete from "+ SI_TABLE("t_ddi_table_info")+ " where ds_id = ? ";
		return CM.getDao().delete("根据数据源id删除表信息", sql, new Object[]{ dsId });
    }
    
	/**
	 * 
	 * @Title: deleteTbInfo
	 * @Description: 删除表信息
	 * @param @param dsId
	 * @param @param tableName
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return int 返回类型
	 * @throws
	 */
    public int deleteTbInfo(String dsId, String tableName) throws DaoException{
    	String sql = "delete from "+ SI_TABLE("t_ddi_table_info")+ " where ds_id = ? and table_name = ? ";
		return CM.getDao().delete("删除表信息", sql, new Object[]{dsId, tableName});
    }

	/**
	 * 
	 * @Title: getTbInfo
	 * @Description: 根据schema、tableName查询表数量
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return int 返回类型
	 * @throws
	 */
    public int getCount(String dsId, String schema, String tableName) throws DaoException{
    	String sql = "select count(*) from "+ SI_TABLE("t_ddi_table_info")+ " where ds_id = ? and schema_name = ? and table_name = ? ";
    	return getDao().queryForInt("根据schema、tableName查询表数量", sql, new Object[]{dsId, schema, tableName});
    }
    
    /**
     * 
     * @MethodName: [getById]
     * @Description:[通过id获取表信息]
     * @Author:     [fxx]
     * @CreateDate: [2017-5-17]
     * @Params:     [ParamType ParamValue,ParamType ParamValue...]
     * @See         [Related classes and methods]
     * @Since       [Product template version]  
     * @Return:     [TableInfoVo]
     * @Throws:     [Exception]
     *
     */
    public TableInfoVo getById(String id) throws DaoException {
    	String sql = "SELECT ID, DS_ID, schema_name, TABLE_NAME, TABLE_COMMENT, TABLE_ORDER, mtime FROM " + SI_TABLE("t_ddi_table_info") + " WHERE ID = ?";
    	return getDao().getModule("获取对象",sql, TableInfoVo.class, new Object[]{id});
    }
    
	/**
	 * 
	 * @Title: getTbNamesByDsId
	 * @Description: 获取表名
	 * @param @param dsId
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return List<String> 返回类型
	 * @throws
	 */
    public List<String> getTbNamesByDsId(String dsId) throws DaoException{
    	String sql = "select TABLE_NAME from " + SI_TABLE("t_ddi_table_info") + " where ds_id = ? ";
    	return CM.getDao().listSignleStringColumn("获取表名", sql, new WhereCondition(), new Object[]{dsId});
    }
}
